package cn.jwcat.controller;

import cn.jwcat.entity.Advise;
import cn.jwcat.service.IAdviseService;
import cn.jwcat.utils.otherUtils;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 建议操作
 */
@RestController
@Transactional
@RequestMapping("/advise")
public class AdviseController {
	@Resource
	IAdviseService adviseService;

	@GetMapping("/{id}")
	@ApiOperation("根据id获取建议信息")
	public Result selectByPrimaryKey(@PathVariable Integer id) {
		return Result.success(adviseService.getById(id));
	}

	@GetMapping("/{page}/{size}")
	@ApiOperation("建议类的分页查询")
	public Result suppliesPage(@PathVariable("page") int page, @PathVariable("size") int size) {
		LambdaQueryWrapper<Advise> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(Advise::getCreateTime).orderByDesc(Advise::getCreateTime);
		PageHelper.startPage(page, size);
		List<Advise> advises = adviseService.list(queryWrapper);
		PageInfo<Advise> advisePageInfo = new PageInfo<>(advises);
		return Result.success(advisePageInfo);
	}

	@PostMapping("/{page}/{size}")
	@ApiOperation("根据建议进行模糊查询")
	public Result inquiry(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody Advise advise) {
		if ("".equals(advise.getStatus()) && "".equals(advise.getContext()) && advise.getTime().length == 0) {
			return Result.error("数据操作失败，请检查输入内容");
		}
		PageHelper.startPage(page, size);
		List<Advise> advises = adviseService.inquiry(advise);
		PageInfo<Advise> messagePageInfo = new PageInfo<>(advises);
		return Result.success(messagePageInfo);
	}

	@PutMapping
	@ApiOperation("提交建议回复")
	public Result updateById(@RequestBody Advise advise) {
		if ("".equals(advise.getStatus()) && "".equals(advise.getReply())) {
			return Result.error("数据操作失败，请检查输入内容");
		}
		boolean bool = adviseService.updateById(advise);
		return otherUtils.dealBool(bool);
	}

	@PostMapping
	@ApiOperation("提交建议信息")
	public Result insert(@RequestBody Advise advise) {
		if ("".equals(advise.getContext())) {
			return Result.error("建议信息内容不能为空，请检查内容");
		}
		boolean bool = adviseService.save(advise);
		return otherUtils.dealBool(bool);
	}
}
